package com.kgc.ljp.dao.impl;

import com.kgc.ljp.dao.FlightDao;
import com.kgc.ljp.dao.SelectDao;
import com.kgc.ljp.entity.Flight;
import com.kgc.ljp.util.Initi;
import com.kgc.ljp.util.SetUtil;
import com.sun.org.apache.xml.internal.security.Init;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class FlightDaoImpl implements FlightDao,SelectDao{
    static Map<String,Flight> flightMap = null;
    static Map<String,Flight> selectMap = new HashMap<>();
    static {
        flightMap = (Map<String, Flight>) SetUtil.readObj("Flight.txt");
        if (flightMap==null){
            flightMap = Initi.flightMap;
        }
        SetUtil.writeObj(flightMap,"Flight.txt");
    }
    @Override
    public boolean addFlight(Flight flight) {
        flightMap = (Map<String, Flight>) SetUtil.readObj("Flight.txt");
        if (flightMap==null){
            flightMap = Initi.flightMap;
        }
        if (!flightMap.containsKey(flight.getaNo())){
            flightMap.put(flight.getaNo(),flight);
            SetUtil.writeObj(flightMap,"Flight.txt");
            return true;
        }
        return false;
    }
    public Map<String,Flight> show(){
        return flightMap;
    }

    @Override
    public Map<String, Flight> selectByCity(String cityName) {
        for (Flight flight:flightMap.values()) {
            if (flight.getfCity().indexOf(cityName)!=-1){
//                selectMap.put(flight.getaNo(),flight);
                flight.print();
            }
        }
        return selectMap;
    }
}
